//
//  MeatFoundApi.m
//  PigMarket
//
//  Created by 张敬 on 2019/9/2.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatFoundApi.h"
#import "Request.h"
#import "RequestHandler.h"
#import "NetworkConfigration.h"
#import "SystemUtil.h"


@implementation MeatFoundApi


+ (instancetype)sharedInstance {
    
    static MeatFoundApi *sharedInstance = nil;
    static dispatch_once_t token;
    
    dispatch_once(&token,^{
        if(sharedInstance == nil){
            sharedInstance = [[MeatFoundApi alloc] init];
        }
    } );
    
    return sharedInstance;
}


#pragma mark - 清饲文章分享
- (RACSignal<FoundActicleShareModel *> *)getArticleShareByArticleId:(NSNumber *)qsArticleId{

    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLFound_Share];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"qsArticleId":qsArticleId};
    request.responseType = [FoundActicleShareModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 清饲文章类型
- (RACSignal <FoundActicleTypeModel *> *)getArticleTypeList{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLFound_ArticleType];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeMember;
    request.responseType = [FoundActicleTypeModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 点赞/取消点赞清饲文章
- (RACSignal<NSNull *> *)getArticleLikeOrUnLikeByParams:(FoundLikeOrUnLikeParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLFound_LikeOrUnLike];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 清清饲文章列表
- (RACSignal<FoundActicleListModel *> *)getArticleDetailListByParams:(FoundActicleListParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLFound_ArticleDetail];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeNone;
    request.param = parmas;
    request.responseType = [FoundActicleListModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 清饲文章收藏
- (RACSignal <NSNull *> *)getArticleCollectionById:(NSNumber *)qsArticleId{
    
    Request *request = [[Request alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/%@/%@", URLFound_ArticleCollection, qsArticleId, kMemberId];
    request.URLString = [self getFullURLString:url];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeMember;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 评论清饲文章
- (RACSignal<NSNull *> *)getArticleCommentByParams:(FoundCommentParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLFound_ArticleComments];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}

#pragma mark - 清饲文章的评论列表
- (RACSignal<FoundCommentListModel *> *)getArticleCommentListByParams:(FoundCommentParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLFound_ArticleCommentList];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = [FoundCommentListModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 点赞/取消点赞评论
- (RACSignal<NSNull *> *)getArticleLikeOrUnlikeByParams:(FoundLikeOrUnLikeParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLFound_ArticleCommentLikeOrUnLike];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}

#pragma mark - 清饲文章收藏列表
- (RACSignal <FoundCommentListModel *> *)getArticleCollectionList{
    
    Request *request = [[Request alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/%@", URLFound_ArticleCollectionList, kMemberId];
    request.URLString = [self getFullURLString:url];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeMember;
    request.responseType = [FoundCommentListModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 取消清饲文章收藏
- (RACSignal<NSNull *> *)getArticleCancelByArticleId:(NSNumber *)qsArticleId{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLFound_ArticleCollectionCancel];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"qsArticleId":qsArticleId, @"memberId":kMemberId};
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 地图显示
- (RACSignal<FoundMapShowModel *> *)getMapShowByLat:(NSString *)latitude lon:(NSString *)longitude{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLFound_MapShow];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"latitude":latitude, @"longitude":longitude};
    request.responseType = [FoundMapShowModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 附近服务商
- (RACSignal<FoundMapNearByModel *> *)getMapNearByByParams:(FoundMapNearByParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLFound_MapNearBy];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = [FoundMapNearByModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 加入/退出服务商
- (RACSignal<NSNull *> *)getMapExitServiceByParams:(FoundServiceExitParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLFound_MapInPutOrOut];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - Other

- (NSString *)getFullURLString:(NSString *)URLString {
    NetworkConfigration *configration = [NetworkConfigration defaultNetworkConfiguration];
    return [NSString stringWithFormat:@"%@%@", configration.baseApiURL, URLString];
}


- (RACSignal *)createSignalWithReuqest:(Request *) request {
    
    
    return [[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        
        RequestHandler *handler = [[RequestHandler alloc] initWithRequest:request responseCallback:^(Response *response) {
            
            
#pragma mark - 回参数
            if (response.code == ResultCodeSuccess) {
                
                if(request.responseType) {
                    [subscriber sendNext:response.data];
                } else {
                    [subscriber sendNext:[NSNull null]];
                }
                [subscriber sendCompleted];
                
            } else {
                if (response.code == ResultCodeDeviceChanged) {//更换设备
                    UserInfo *userInfo = [UserInfoDataProvider sharedInstance].userInfo;
                    userInfo.isLogin = NO;
                    [UserInfoDataProvider sharedInstance].userInfo = userInfo;
                    
                }
                NSError *error = [NSError errorWithDomain:@"com.naturalgrowth.pigmarket.ErrorDomain" code:response.code userInfo: @{ NSLocalizedDescriptionKey : response.msg }];
                [subscriber sendError:error];
            }
            
        }];
        
        [handler sendRequest];
        
        return [RACDisposable disposableWithBlock:^{
            
            [handler cancelRequest];
        }];
    }] subscribeOn:[RACScheduler scheduler]] ;
}


@end


